<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:30 AM
 */

namespace app\index\controller;

use app\common\model\ToolModel;
use app\common\YujisoController;
use app\index\service\CompanyService;
use think\Exception;

class Company extends YujisoController
{
    public $param;
    public function __construct()
    {
        parent::__construct();
        $temp = $this->request->param();
        if (isset($temp['s'])) unset($temp['s']);
        $this->param = $temp;
    }

    /**
     * 获取公司列表
     * @author duanwenjie
     */
    public function getCompanyList()
    {
        $param = $this->param;
        $result = CompanyService::getCompanyList($param);
        returnJson('1','查询成功',$result['list'],$result['total']);
    }


    /**
     * 启用/禁用
     * @author duanwenjie
     */
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'base_company');
        returnJson(1,'操作成功');
    }

    /**
     * 新增/编辑
     * @author duanwenjie
     */
    public function edit()
    {
       try{
           $param = $this->param;
           $id = "";
           if ($this->request->isPost()){
               /*编辑*/
               if (isset($param['id']) && !empty($param['id'])){
                   $id = $param['id'];
                   unset($param['id']);
               }
               $res = CompanyService::checkData($param, $id);
               $param['base_country_id'] = $res['base_country_id']; // 国家ID
               $param['base_country_name'] = $res['base_country_name']; // 国家名称
               if (!empty($id)){
                   CompanyService::updateShop($id,$param['base_country_id'],$param['base_country_name']);
                   ToolModel::updateData(['id' => $id], $param, 'base_company');
               }else{
                   ToolModel::insertData($param, 'base_company');
               }
               returnJson(1, "操作成功");
           }else{
               $result = CompanyService::getCompanyList($param);
               returnJson(1, '', $result['list']);
           }
       }catch (Exception $e){
           trace($e->getMessage(), 'error');
           returnJson(500, '失败:'.$e->getMessage());
       }
    }
}